﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HHL_Acc.HHL_BussAcc.DataObject;
using HHL_Acc.HHL_EntityAcc;
using HHL_Acc.HHL_Util;
using HHL_Util;
using System.Timers;

namespace HHL_Acc
{
    public partial class BanVeThangFRM : Form
    {
        private bool edit = false;

        public BanVeThangFRM()
        {
            InitializeComponent();
            InitSoldTickets();
        }

        public void InitSoldTickets()
        {
            disableText();
            dtFromdate_DSVDB.Value = new DateTime(CommonConstant.TICK_DATE_MIN);
            List<ColumnItem> columnItemsList = new List<ColumnItem>();
            columnItemsList.Add(new ColumnItem("Idx", "STT", 60));
            columnItemsList.Add(new ColumnItem("Serial", "Serial", 120));
            columnItemsList.Add(new ColumnItem("TicketPrice", "Giá", 100));
            columnItemsList.Add(new ColumnItem("TicketIDNameUnicode", "Loại vé", 100));
            columnItemsList.Add(new ColumnItem("StationID", "Trạm", 80));
            columnItemsList.Add(new ColumnItem("VehicleIDNameUnicode", "Loại xe ĐK", 160));
            columnItemsList.Add(new ColumnItem("Start_Date", "Ngày bắt đầu", 100));
            columnItemsList.Add(new ColumnItem("End_Date", "Ngày hết hạn", 100));
            columnItemsList.Add(new ColumnItem("PlateNumber", "Biển số", 100));
            columnItemsList.Add(new ColumnItem("RegisNumber", "Số đăng ký", 100));
            columnItemsList.Add(new ColumnItem("Notes", "Ghi chú", 150));
            columnItemsList.Add(new ColumnItem("VehicleOwner", "Chủ PT", 140));
            columnItemsList.Add(new ColumnItem("AddressUnicode", "Địa chỉ", 150));
            columnItemsList.Add(new ColumnItem("Tel", "Số ĐT", 100));
            CommonGuiUtil.CreateColumns(ref gvListSold_DSVDB, columnItemsList);

            SmiSoldMonthlyTicketsBus bus = new SmiSoldMonthlyTicketsBus();
            gvListSold_DSVDB.DataSource = bus.GetSmiSoldMonthlyTickets();
        }

        private void bntAdd_DSVDB_Click(object sender, EventArgs e)
        {
            tabThongTinChiTietBanVe.Select();
            tabControl1.SelectedIndex = 1;
        }

        private void BtnSearchSerial_Details_Click(object sender, EventArgs e)
        {
            int intTicket, intVehicle;
            string strTicketCode;
            if (CommonBusUtil.IsSerial(txtSerial_Details.Text) == false)
                return;
            //Kiểm tra vé đó có nằm trong kho vé của nhân viên không
            SmiSoldMonthlyTicketsBus bus = new SmiSoldMonthlyTicketsBus();
            if (bus.CheckOwnerSerial(txtSerial_Details.Text, "0000") == false)
            {
                MessageBox.Show("Vé không nằm trong kho vé của nhân viên");
                strTicketCode = CommonBusUtil.GetSerialTicketCode(txtSerial_Details.Text);
                intVehicle = CommonBusUtil.GetSerialVehicleID(txtSerial_Details.Text);
                intTicket = CommonBusUtil.GetSerialTicketID(txtSerial_Details.Text);
                txtPrice_Details.Text = AppInfo.GetPriceFromTicket(intTicket, intVehicle);
                txtTicketID_Details.Text = AppInfo.GetTicketName(intTicket);
                txtVehicleName_Details.Text = AppInfo.GetVihicleName(intVehicle);
                dtStartdate_Details.Value = DateTime.Now;
                dtStartdate_Details.Refresh();
                dtEnddate_Details.Value = (DateTime.Parse((dtStartdate_Details.Value).ToString())).AddMonths(1);
                return;
            }
            else
            {
                //CommonBusUtil.GetSerialTicketCode CommonBusUtil.GetSerialTicketCode()
                strTicketCode = CommonBusUtil.GetSerialTicketCode(txtSerial_Details.Text);
                intVehicle = CommonBusUtil.GetSerialVehicleID(txtSerial_Details.Text);
                intTicket = CommonBusUtil.GetSerialTicketID(txtSerial_Details.Text);
                txtPrice_Details.Text =
                    txtTicketID_Details.Text = AppInfo.GetTicketName(intTicket);
                txtVehicleName_Details.Text = AppInfo.GetVihicleName(intVehicle);
                dtStartdate_Details.Value = DateTime.Now;
                dtStartdate_Details.Refresh();
                dtEnddate_Details.Value = (DateTime.Parse((dtStartdate_Details.Value).ToString())).AddMonths(1);
            }
        }

        private void btnSold_Details_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnSold_Details.Text == "Bán vé")
                {
                    soldButton();
                    enableText();
                    ResetText();
                }
                else
                {
                    SmiSoldMonthlyTicketsBus bus = new SmiSoldMonthlyTicketsBus();

                    cancleButton();
                    disableText();
                    if (edit == false)
                    {
                        //Thêm vé mới vào trong cơ sở dữ liệu
                        SmiSoldMonthlyTickets soldMonthlyTickets = new SmiSoldMonthlyTickets();
                        soldMonthlyTickets.SoldID = CommonUtil.MakeStringID();
                        soldMonthlyTickets.Login = AppInfo.GenLogin.Login;
                        soldMonthlyTickets.Serial = txtSerial_Details.Text;
                        soldMonthlyTickets.Sold_Date = DateTime.Now;
                        soldMonthlyTickets.Sold_Hour = (DateTime.Now.Hour*60 + DateTime.Now.Minute)*60 +
                                                       DateTime.Now.Second;
                        soldMonthlyTickets.Start_Date = dtStartdate_Details.Value;
                        soldMonthlyTickets.End_Date = dtEnddate_Details.Value;
                        soldMonthlyTickets.PlateNumber = txtPlateNumber_Details.Text;
                        soldMonthlyTickets.TicketCode = CommonBusUtil.GetSerialTicketCode(txtSerial_Details.Text);
                        soldMonthlyTickets.TicketPrice =CommonUtil.ConvertStringToCurrency(txtPrice_Details.Text.ToString());
                        soldMonthlyTickets.Status = 0;
                        soldMonthlyTickets.Disabled = 0;
                        soldMonthlyTickets.RegisNumber = txtRegisNumber_Details.Text;
                        soldMonthlyTickets.Notes = txtNote_Details.Text;
                        soldMonthlyTickets.VehicleOwner = txtVehicleOwner_Details.Text;
                        soldMonthlyTickets.Address = txtAddress_Details.Text;
                        soldMonthlyTickets.Tel = txtTel_Details.Text;
                        soldMonthlyTickets.VehicleID = CommonBusUtil.GetSerialVehicleID(txtSerial_Details.Text);
                        soldMonthlyTickets.TicketID = CommonBusUtil.GetSerialTicketID(txtSerial_Details.Text);
                        soldMonthlyTickets.StationID = 1;
                        if (bus.InsertSmiSoldMonthlyTickets(soldMonthlyTickets) == 1)
                        {
                            bus.DeleteSmiMonthlyTicket(txtSerial_Details.Text);
                        }
                        gvListSold_DSVDB.DataSource = bus.GetSmiSoldMonthlyTickets();
                        ResetText();
                        MessageBox.Show("Bán vé thành công");
                    }
                    else // Chỉnh sữa thông tin bán vé
                    {
                        SmiSoldMonthlyTickets soldMonthlyTickets = new SmiSoldMonthlyTickets();
                        soldMonthlyTickets.SoldID = CommonUtil.MakeStringID();
                        soldMonthlyTickets.Login = AppInfo.GenLogin.Login;
                        soldMonthlyTickets.Serial = txtSerial_Details.Text;
                        soldMonthlyTickets.Sold_Date = DateTime.Now;
                        soldMonthlyTickets.Sold_Hour = (DateTime.Now.Hour*60 + DateTime.Now.Minute)*60 +
                                                       DateTime.Now.Second;
                        soldMonthlyTickets.Start_Date = dtStartdate_Details.Value;
                        soldMonthlyTickets.End_Date = dtEnddate_Details.Value;
                        soldMonthlyTickets.PlateNumber = txtPlateNumber_Details.Text;
                        soldMonthlyTickets.TicketCode = CommonBusUtil.GetSerialTicketCode(txtSerial_Details.Text);
                        soldMonthlyTickets.TicketPrice =
                            CommonUtil.ConvertStringToCurrency(txtPrice_Details.Text.ToString());
                        soldMonthlyTickets.Status = 0;
                        soldMonthlyTickets.Disabled = 0;
                        soldMonthlyTickets.RegisNumber = txtRegisNumber_Details.Text;
                        soldMonthlyTickets.Notes = txtNote_Details.Text;
                        soldMonthlyTickets.VehicleOwner = txtVehicleOwner_Details.Text;
                        soldMonthlyTickets.Address = txtAddress_Details.Text;
                        soldMonthlyTickets.Tel = txtTel_Details.Text;
                        soldMonthlyTickets.VehicleID = CommonBusUtil.GetSerialVehicleID(txtSerial_Details.Text);
                        soldMonthlyTickets.TicketID = CommonBusUtil.GetSerialTicketID(txtSerial_Details.Text);
                        soldMonthlyTickets.StationID = 1;
                        bus.UpdateSmiSoldMonthlyTickets(soldMonthlyTickets);
                        gvListSold_DSVDB.DataSource = bus.GetSmiSoldMonthlyTickets();
                        ResetText();
                        MessageBox.Show("Chỉnh sữa thành công");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ResetText()
        {
            txtSerial_Details.Text = "";
            txtPrice_Details.Text = "";
            txtTicketID_Details.Text = "";
            txtVehicleName_Details.Text = "";
            dtStartdate_Details.Value = DateTime.Now;
            dtEnddate_Details.Value = DateTime.Now;
            txtPlateNumber_Details.Text = "";
            txtRegisNumber_Details.Text = "";
            txtNote_Details.Text = "";
            txtVehicleOwner_Details.Text = "";
            txtAddress_Details.Text = "";
            txtTel_Details.Text = "";
        }

        private void btnEdit_Details_Click(object sender, EventArgs e)
        {
            if (btnEdit_Details.Text == "Chỉnh sữa")
            {
                edit = true;
                soldButton();
                enableText();
            }
            else
            {
                cancleButton();
                disableText();
                ResetText();
            }
        }

        private void gvListSold_DSVDB_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int intTicketCode, intVehicle, intTicket;
                int row = e.RowIndex;
                if (row < 0)
                {
                    return;
                }
                SmiSoldMonthlyTickets smisoldMonthlyTickets = new SmiSoldMonthlyTickets();
                txtSerial_Details.Text = gvListSold_DSVDB.Rows[row].Cells["Serial"].Value.ToString();
                txtPrice_Details.Text = gvListSold_DSVDB.Rows[row].Cells["TicketPrice"].Value.ToString();
                txtTicketID_Details.Text = gvListSold_DSVDB.Rows[row].Cells["TicketIDNameUnicode"].Value.ToString();
                txtVehicleName_Details.Text = gvListSold_DSVDB.Rows[row].Cells["VehicleIDNameUnicode"].Value.ToString();
                dtStartdate_Details.Value =
                    DateTime.Parse(gvListSold_DSVDB.Rows[row].Cells["Start_Date"].Value.ToString());
                dtEnddate_Details.Value = DateTime.Parse(gvListSold_DSVDB.Rows[row].Cells["End_Date"].Value.ToString());
                txtPlateNumber_Details.Text = gvListSold_DSVDB.Rows[row].Cells["PlateNumber"].Value.ToString();
                txtRegisNumber_Details.Text = gvListSold_DSVDB.Rows[row].Cells["RegisNumber"].Value.ToString();
                txtNote_Details.Text = gvListSold_DSVDB.Rows[row].Cells["Notes"].Value.ToString();
                txtVehicleOwner_Details.Text = gvListSold_DSVDB.Rows[row].Cells["VehicleOwner"].Value.ToString();
               
                txtAddress_Details.Text = gvListSold_DSVDB.Rows[row].Cells["AddressUnicode"].Value.ToString();
                txtTel_Details.Text = gvListSold_DSVDB.Rows[row].Cells["Tel"].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gvListSold_DSVDB_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void enableText()
        {
            txtSerial_Details.Enabled = true;
            dtStartdate_Details.Enabled = true;
            txtPlateNumber_Details.Enabled = true;
            txtRegisNumber_Details.Enabled = true;
            txtNote_Details.Enabled = true;
            txtVehicleOwner_Details.Enabled = true;
            txtAddress_Details.Enabled = true;
            txtTel_Details.Enabled = true;
        }

        private void disableText()
        {
            txtSerial_Details.Enabled = false;
            dtStartdate_Details.Enabled = false;
            txtPlateNumber_Details.Enabled = false;
            txtRegisNumber_Details.Enabled = false;
            txtNote_Details.Enabled = false;
            txtVehicleOwner_Details.Enabled = false;
            txtAddress_Details.Enabled = false;
            txtTel_Details.Enabled = false;
        }

        private void soldButton()
        {
            btnSold_Details.Text = "Bán";
            btnEdit_Details.Text = "Không bán";
            btnReceipt_Details.Visible = false;
            btnTicket_Details.Visible = false;
        }

        private void cancleButton()
        {
            btnSold_Details.Text = "Bán vé";
            btnEdit_Details.Text = "Chỉnh sữa";
            btnReceipt_Details.Visible = true;
            btnTicket_Details.Visible = true;
        }

        private void btnSearchSerial_DSVDB_Click(object sender, EventArgs e)
        {

        }

        private void txtNext_ExpList_Click(object sender, EventArgs e)
        {

        }
    }
}